package net.xiangcaowuyu.mybatisplussample.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description:
 *
 * @author : laughing
 * DateTime: 2021-04-22 11:15
 */
@Configuration
public class MybatisPlusConfig {


    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        TenantLineHandler tenantLineHandler = new TenantLineHandler() {

            /**
             * 租户值，正常从上下文获取，这里仅作演示
             * @return
             */
            @Override
            public Expression getTenantId() {
                return new LongValue(1);
            }

            /**
             * 租户列名称
             * @return
             */
            @Override
            public String getTenantIdColumn() {
                return "tenant_id";
            }

            /**
             * 配置哪些表不需要过滤
             * @param tableName 表名
             * @return 返回true，代表不加tenantId条件
             */
            @Override
            public boolean ignoreTable(String tableName) {
                return false;
            }
        };
        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(tenantLineHandler));
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }

}
